<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Service\Dao;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\Question;
use HyperfX\Utils\Service;

class QuestionDao extends Service
{
    public function first(int $id, $throw = false): ?Question
    {
        $model = Question::query()->find($id);
        if (empty($model) && $throw) {
            throw new BusinessException(ErrorCode::QUESTION_NOT_EXIST);
        }
        return $model;
    }

    public function firstCache(int $id, $throw = false): ?Question
    {
        $model = Question::findFromCache($id);
        if (empty($model) && $throw) {
            throw new BusinessException(ErrorCode::QUESTION_NOT_EXIST);
        }
        return $model;
    }

    /**
     * @param $ids
     * @return \Hyperf\Database\Model\Collection|Question[]
     */
    public function findMany(array $ids)
    {
        return Question::findManyFromCache($ids);
    }
}
